//
//  YAYIAddResumeController.m
//  YAYIDoctor
//
//  Created by hxp on 16/6/24.
//
//

#import "YAYIAddResumeController.h"
#import "YAYIEditResumeController.h"
#import "YAYIResumeViewController.h"
#import "YAYIGetSystemPicView.h"
#import "YAYIResumeCell.h"
#import "Masonry/Masonry.h"
#define YAYIFont(t) [UIFont systemFontOfSize:t]

@interface YAYIAddResumeController ()<UITableViewDelegate,UITableViewDataSource,YAYIResumeCellDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong,nonatomic)YAYIResumeViewController *editResumeView;
@property (nonatomic,assign)Boolean isFirst;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)UILabel *mobileLabel;
@property (nonatomic,strong)UILabel *workLabel;
@property (nonatomic,strong)UILabel *addressLabel;
@property (nonatomic,strong)UILabel *workExperise;
@property (nonatomic,strong)UILabel *line;
@property (nonatomic,strong)UILabel *titleView;
@property (nonatomic,strong)UIView  *footView;
@property (nonatomic,strong)NSMutableArray *imageAry;
@property (nonatomic,strong)UIView *bgView;
@property (nonatomic,strong)UILabel *placeholder;
@property (nonatomic,strong)UILabel *count;
@property (nonatomic,strong)UIPickerView *pickerView;
@property (nonatomic,strong)UIView *popView;
@property (nonatomic,strong)UIControl *control;
@property (nonatomic,strong)NSString *work;
@property (nonatomic,strong)NSString *education;
@property (nonatomic,strong)NSString *workyears;
@property (nonatomic,strong)UIButton *addImageBtn;
//@property (nonatomic,strong)NSString *;

// 保存底下UITextView
@property (nonatomic, weak) UITextView *educationTV;
@property (nonatomic, weak) UITextView *workTV;

// 提交参数
@property (nonatomic, assign) NSInteger workYear;

@property (nonatomic, assign) NSInteger oldImageRowCount;

@end

@implementation YAYIAddResumeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"编辑简历";
    
    _isFirst = true;
    self.imageAry = [NSMutableArray array];
    
    [self createRightBtn];
    self.view.backgroundColor = [UIColor blueColor];
    [self getDoctoInfo];
    [self createTableView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


-(void)createTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self createFooterView];
    self.tableView.tableHeaderView = [self headerView];
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    [self dataSource];
}


-(void)createRightBtn{
    
    NSString *rightBtnTitleStr = @"提交";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button setTitle:rightBtnTitleStr forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    
    button.frame = (CGRect){{0, 0}, CGSizeMake(70, 30)};
    
    // 让按钮内部的所有内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
    
    // 修改导航栏左边的item
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}


-(UIView *)headerView{
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    bgView.frame  = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width ,220);
    
    UIView *headerView = [UIView new];
    headerView.frame = CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 210);
    headerView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:headerView];
    UILabel *dotLabel = [UILabel new];
    dotLabel.frame = CGRectMake(0, 4, 4, 30);
    dotLabel.backgroundColor = [UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1];
    [headerView addSubview:dotLabel];
    
    UILabel *title = [UILabel new];
    title.text = @"基本信息";
    title.frame = CGRectMake(CGRectGetMaxX(dotLabel.frame) + 15, CGRectGetMinY(dotLabel.frame), 100, 30);
    title.textColor = YAYIColor(83,165, 243);
    title.font = YAYIFont(14);
    [headerView addSubview:title];
    
    UILabel *line = [UILabel new];
    line.frame = CGRectMake(0, CGRectGetMaxY(dotLabel.frame) + 4, [UIScreen mainScreen].bounds.size.width, 0.2);
    line.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:line];
    
    // 姓名
    self.nameLabel = [UILabel new];
    self.nameLabel.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(line.frame) + 8, 200, 30);
    self.nameLabel.font = YAYIFont(14);
    self.nameLabel.textColor = YAYIColor(122, 123,125);
    [headerView addSubview:self.nameLabel];
    
    // 电话
    self.mobileLabel = [UILabel new];
    self.mobileLabel.font = YAYIFont(14);
    self.mobileLabel.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(self.nameLabel.frame)+4, 180, 30);
    self.mobileLabel.textColor = YAYIColor(122, 123,125);
    [headerView addSubview:self.mobileLabel];
    
    // 职务
    self.workLabel = [UILabel new];
    self.workLabel.font = YAYIFont(14);
    self.workLabel.textColor = YAYIColor(122, 123,125);
    self.workLabel.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(self.mobileLabel.frame), 180, 30);
    [headerView addSubview:self.workLabel];
    
    // 工作地点
    self.addressLabel = [UILabel new];
    self.addressLabel.textColor = YAYIColor(122, 123,125);
    self.addressLabel.font = YAYIFont(14);
    self.addressLabel.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(self.workLabel.frame), 280, 30);
    [headerView addSubview:self.addressLabel];
    
    self.workExperise = [UILabel new];
    self.workExperise.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(self.addressLabel.frame), SCREEN_width - 40, 30);
    self.workExperise.font = YAYIFont(14);
    self.workExperise.userInteractionEnabled = YES;
    self.workExperise.textColor = YAYIColor(122, 123,125);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectWorkLimit:)];
    [self.workExperise addGestureRecognizer:tap];
    [headerView addSubview:self.workExperise];
    
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_width - 20 - 7, CGRectGetMinY(self.workExperise.frame)+6, 7, 12)];
    icon.centerY = self.workExperise.centerY;
    
    icon.image = [UIImage imageNamed:@"accessIcon"];
    [headerView addSubview:icon];
    return bgView;
}


-(void)dataSource{
    self.nameLabel.text =    @"姓       名：";
    self.mobileLabel.text =  @"电       话：";
    self.workLabel.text =    @"职       务：";
    self.addressLabel.text = @"工作地点：";
    self.workExperise.text = @"工作年限：请选择";
}

-(void)createFooterView{
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    bgView.clipsToBounds = YES;
    bgView.layer.masksToBounds = YES;
    self.bgView = bgView;
    bgView.frame  = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width ,210);
    self.tableView.tableFooterView = bgView;
    
    
    UIView *footerView = [UIView new];
    footerView.backgroundColor = [UIColor whiteColor];
    footerView.frame = CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width ,200 );
    footerView.clipsToBounds = YES;
    footerView.layer.masksToBounds = YES;
    self.footView = footerView;
    [bgView addSubview:footerView];
    
    UILabel *dotLabel = [UILabel new];
    dotLabel.frame = CGRectMake(0, 4, 4, 30);
    dotLabel.backgroundColor = [UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1];
    [footerView addSubview:dotLabel];
    
    UILabel *title = [UILabel new];
    title.text = @"相关证书";
    title.frame = CGRectMake(CGRectGetMaxX(dotLabel.frame) + 20, CGRectGetMinY(dotLabel.frame), 100, 30);
    title.textColor = YAYIColor(83,165, 243);
    title.font = YAYIFont(14);
    [footerView addSubview:title];
    
    UILabel *line = [UILabel new];
    line.frame = CGRectMake(0, CGRectGetMaxY(title.frame) + 4, [UIScreen mainScreen].bounds.size.width, 0.2);
    self.line = line;
    line.backgroundColor = [UIColor lightGrayColor];
    [footerView addSubview:line];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, CGRectGetMaxY(line.frame) + 10, 60, 60);
//    button.backgroundColor = [UIColor orangeColor];
    [button setBackgroundImage:[UIImage imageNamed:@"addPhoto"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(upLoadPhoto:) forControlEvents:UIControlEventTouchUpInside];
    self.addImageBtn = button;
    [footerView addSubview:button];
    
    UILabel *label = [UILabel new];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor lightGrayColor];
    label.text = @"请上传你的教育经历相关证书及医师资格证书、职业资格证。";
    
    CGSize lblSize = [label.text sizewithFont:label.font maxSize:CGSizeMake(SCREEN_width - 20, CGFLOAT_MAX)];
    label.frame = CGRectMake(10, CGRectGetMaxY(button.frame) + 10, SCREEN_width - 20, lblSize.height);
    [footerView addSubview:label];
    self.titleView = label;
    
    footerView.height = CGRectGetMaxY(label.frame) + 10;
    bgView.height = CGRectGetMaxY(label.frame) + 20;
    
    self.tableView.tableFooterView = bgView;
    self.oldImageRowCount = 1;
}


-(void)upLoadPhoto:(UIButton *)sender{
    
    YAYIGetSystemPicView *picView = [[YAYIGetSystemPicView alloc] initWithFrame:CGRectZero viewController:self];
    [picView openSystemPhotoBlock:^(UIImage *image) {
        [self.imageAry addObject:image];
        CGFloat width = ([UIScreen mainScreen].bounds.size.width - 10 * 2)/5.0;
        int i =0;
        for (UIView *view in self.footView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                [view removeFromSuperview];
            }
        }
        for (UIImage *image in self.imageAry) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+i%5 * width, CGRectGetMaxY(self.line.frame) + 10 +(i/5 * 70), 60, 60)];
            imageView.clipsToBounds = YES;
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.image = image;
            imageView.userInteractionEnabled = YES;
            [self.footView addSubview:imageView];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 0, 20, 20);
            button.tag = i;
            [button setImage:[UIImage imageNamed:@"deleteIcon"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];
            i++;
        }
        if (i !=9) {
            
            sender.frame = CGRectMake(10 + (self.imageAry.count%5 * width),CGRectGetMaxY(self.line.frame) + 10 + (self.imageAry.count/5 * 70) , 60, 60);
            
            NSInteger rowCount = (self.imageAry.count + 1 - 1) / 5 + 1;
            self.bgView.height += (rowCount - self.oldImageRowCount) * 70;
            self.footView.height += (rowCount - self.oldImageRowCount) * 70;
            
            CGRect rectList = self.tableView.frame;
            rectList.size.height = rectList.size.height ;
            self.titleView.y = CGRectGetMaxY(sender.frame) + 10;
//            self.tableView.contentSize = CGSizeMake(SCREEN_width, self.tableView.contentSize.height + (rowCount - self.oldImageRowCount ) * 70);
            self.tableView.tableFooterView = self.bgView;
            
            self.oldImageRowCount = rowCount;
        }else{
            
            sender.frame = CGRectMake(10 + (self.imageAry.count%5 * width),CGRectGetMaxY(self.line.frame) + 10 + (self.imageAry.count/5 * 70) , 60, 60);
            
            NSInteger rowCount = (self.imageAry.count + 1 - 1) / 5 + 1;
            self.bgView.height += (rowCount - self.oldImageRowCount) * 70;
            self.footView.height += (rowCount - self.oldImageRowCount) * 70;
            
            CGRect rectList = self.tableView.frame;
            rectList.size.height = rectList.size.height ;
            self.titleView.y = CGRectGetMaxY(sender.frame) + 10;
            
//            self.tableView.contentSize = CGSizeMake(SCREEN_width, self.tableView.contentSize.height + (rowCount - self.oldImageRowCount ) * 70);
            self.tableView.tableFooterView = self.bgView;
            
            self.oldImageRowCount = rowCount;
        }
    }];
    [self.view addSubview:picView];
}


-(void)createPickerView{
    
    self.control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.control.backgroundColor = [UIColor colorWithWhite:0  alpha:0.2];
    [self.control addTarget:self action:@selector(dismissAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.control];
    
    self.popView = [UIView new];
    self.popView.backgroundColor = [UIColor whiteColor];
    self.popView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 260);
    [self.control addSubview:self.popView];
    
    // 确定、取消
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.tag = 100;
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:YAYIColor(103, 176, 254) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [cancelBtn setTitleColor:YAYIColor(103, 176, 254) forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [self.popView addSubview:cancelBtn];
    [cancelBtn addTarget:self action:@selector(onSelectWorkYearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
    }];
    
    // 添加完成按钮
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.tag = 200;
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:YAYIColor(103, 176, 254) forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [sureBtn setTitleColor:YAYIColor(103, 176, 254) forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [self.popView addSubview:sureBtn];
    [sureBtn addTarget:self action:@selector(onSelectWorkYearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(30);
    }];
    
    
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 60)/2.0, 0,60, 220)];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.pickerView reloadAllComponents];
    [self.popView addSubview:self.pickerView];
    
    // 标题
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"选择工作年限";
    titleLbl.textColor = YAYIColor(50, 50, 50);
    titleLbl.backgroundColor = [UIColor whiteColor];
    titleLbl.font = [UIFont systemFontOfSize:15];
    [self.popView addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.centerX.mas_equalTo(self.popView);
        make.height.mas_equalTo(30);
    }];
}



#pragma mark - 获取数据
-(void)commit{
    
    //判断是参数否为空
    if (self.workYear <= 0) {
        [SVProgressHUD showImage:nil status:@"\n\n请选择工作年限\n\n"];
        return;
    }
    
    if (self.education.length == 0) {
        [SVProgressHUD showImage:nil status:@"\n\n请填写教育经历\n\n"];
        return;
    }
    
    if (self.work.length == 0) {
        [SVProgressHUD showImage:nil status:@"\n\n请填写工作经历\n\n"];
        return;
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [[NSSet  alloc] initWithObjects:@"application/json", nil];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",API, insertResume_url];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"work"] = self.work;
    param[@"education"] = self.education;
    param[@"workyears"] = @(self.workYear);
    
    YAYILog(@"param:%@", param);
    
    [manager POST:urlStr parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        int i =0;
        for (UIImage *image in self.imageAry) {
            NSData *data =UIImageJPEGRepresentation(image,0.5);
            [formData appendPartWithFileData:data name:[NSString stringWithFormat:@"uploadFile%d.jpg",i] fileName:[NSString stringWithFormat:@"uploadFile%d.jpg",i] mimeType:@"image/png"];
            i++;
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        [uploadProgress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [SVProgressHUD showSuccessWithStatus:responseObject[@"message"]];
        __block typeof(self) weakself = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            if (weakself.backOperation) {
                weakself.backOperation();
            }
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] options:NSJSONReadingMutableContainers error:nil];
        
        [SVProgressHUD showErrorWithStatus:dict[@"message"]];
    }];
}


-(void)getDoctoInfo{
    
    [YAYINetWorking getWithURL:me_personalinfo param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            
            YAYILog(@"返回：%@", dict);
            NSDictionary *data = dict[@"data"];
//            self.nameLabel.text =    [NSString stringWithFormat:@"姓       名：%@",data[@"fullname"]];
//            self.mobileLabel.text =  [NSString stringWithFormat:@"电       话：%@",data[@"mobile"]];
//            self.workLabel.text =    [NSString stringWithFormat:@"职       务：%@",data[@"title"]];
//            self.addressLabel.text = [NSString stringWithFormat:@"工作地点：%@", data[@"defaulthosname"]];
            self.nameLabel.text =    [NSString stringWithFormat:@"姓       名：%@",data[@"fullname"]];
            self.mobileLabel.text =  [NSString stringWithFormat:@"电       话：%@",data[@"mobile"]];
            self.workLabel.text =    [NSString stringWithFormat:@"职       务：%@",data[@"title"]];
            self.addressLabel.text = [NSString stringWithFormat:@"工作地点：%@", data[@"defaulthosname"]];
        }
        
    }];
    
}


#pragma mark - 点击事件
-(void)switchView:(UIButton *)sender{
//    if (_isFirst) {
//        self.editResumeView = [YAYIResumeViewController  new];
//        [self addChildViewController:_editResumeView];
//        [self.view addSubview:self.editResumeView.view];
//        
//        [sender setTitle:@"修改" forState:UIControlStateNormal];
        [self.view endEditing:YES];
        _isFirst = false;
        
        [self commit];
        return;
//    }else{
//        /*
//         [_editResumeView removeFromParentViewController];
//         [_editResumeView.view removeFromSuperview];
//         [sender setTitle:@"提交" forState:UIControlStateNormal];
//         _isFirst = true;
//         */
//        YAYIEditResumeController *edit = [YAYIEditResumeController new];
//        [self.navigationController pushViewController:edit animated:YES];
//        return ;
//    }
}

-(void)deleteAction:(UIButton *)sender{

    [self.imageAry removeObjectAtIndex:sender.tag];
    int i =0;
    [self.view layoutIfNeeded];
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - 10 * 2)/5.0;
    for (UIView *view in self.footView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
    for (UIImage *image in self.imageAry) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+i%5 * width, CGRectGetMaxY(self.line.frame) + 10 +(i/5 * 70), 60, 60)];
        imageView.image = image;
        imageView.userInteractionEnabled = YES;
        [self.footView addSubview:imageView];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 20, 20);
//        button.backgroundColor = [UIColor redColor];
        button.tag = i;
        [button setImage:[UIImage imageNamed:@"deleteIcon"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
        [imageView addSubview:button];
        i++;
    }
    if (i !=9) {
        
        self.addImageBtn.frame = CGRectMake(10 + (self.imageAry.count%5 * width),CGRectGetMaxY(self.line.frame) + 10 + (self.imageAry.count/5 * 70) , 60, 60);
        
        NSInteger rowCount = (self.imageAry.count + 1 - 1) / 5 + 1;
        self.bgView.height += (rowCount - self.oldImageRowCount) * 70;
        self.footView.height += (rowCount - self.oldImageRowCount) * 70;
        
        CGRect rectList = self.tableView.frame;
        rectList.size.height = rectList.size.height ;
        self.titleView.y = CGRectGetMaxY(self.addImageBtn.frame) + 10;
//        self.tableView.contentSize = CGSizeMake(SCREEN_width, self.tableView.contentSize.height + (rowCount - self.oldImageRowCount ) * 70);
        self.tableView.tableFooterView = self.bgView;
        
        self.oldImageRowCount = rowCount;
    }else{
        
        self.addImageBtn.frame = CGRectMake(10 + (self.imageAry.count%5 * width),CGRectGetMaxY(self.line.frame) + 10 + (self.imageAry.count/5 * 70) , 60, 60);
        
        NSInteger rowCount = (self.imageAry.count + 1 - 1) / 5 + 1;
        self.bgView.height += (rowCount - self.oldImageRowCount) * 70;
        self.footView.height += (rowCount - self.oldImageRowCount) * 70;
        
        CGRect rectList = self.tableView.frame;
        rectList.size.height = rectList.size.height ;
        self.titleView.y = CGRectGetMaxY(self.addImageBtn.frame) + 10;
//        self.tableView.contentSize = CGSizeMake(SCREEN_width, self.tableView.contentSize.height + (rowCount - self.oldImageRowCount ) * 70);
        self.tableView.tableFooterView = self.bgView;
        
        self.oldImageRowCount = rowCount;
    }
    
}


- (void)onSelectWorkYearBtnClick:(UIButton *)sender
{
    if (sender.tag == 100) { // 取消
        
        [self dismissAction:self.control];
    }else{ // 取消
    
        NSInteger row = [self.pickerView selectedRowInComponent:0];
        self.workExperise.text = [NSString stringWithFormat:@"工作年限：%ld",row + 1];
        self.workYear = row + 1;
        [self dismissAction:self.control];
    }
}


-(void)selectWorkLimit:(UITapGestureRecognizer *)tap{
    
    if (self.popView != nil) {
        CGRect rect = self.popView.frame;
        rect.origin.y += 220;
        [UIView animateWithDuration:0.3 animations:^{
            self.popView.frame = rect;
        } completion:^(BOOL finished) {
            [self.popView removeFromSuperview];
            self.popView = nil;
        }];
        
    }else{
        [self selectWorkYearLimit];
    }
    
}


-(void)selectWorkYearLimit{
    
    [self createPickerView];
    CGRect rect = self.popView.frame;
    rect.origin.y -= 220;
    [UIView animateWithDuration:0.25 animations:^{
        self.popView.frame = rect;
    }];
}


-(void)dismissAction:(UIControl *)control{
    CGRect rect = self.popView.frame;
    rect.origin.y += 220;
    [UIView animateWithDuration:0.3 animations:^{
        self.popView.frame = rect;
    } completion:^(BOOL finished) {
        [self.popView removeFromSuperview];
        self.popView = nil;
        
        [control removeFromSuperview];
        self.control = nil;
    }];
}


#pragma mark - 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer = @"cell";
    YAYIResumeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[YAYIResumeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    cell.index = indexPath.row;
    cell.delegate = self;
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"教育经历";
        cell.placeholder.text = @"请填写你的培训学经历";
        
        
    }else{
        cell.titleLabel.text = @"工作经历";
        cell.placeholder.text = @"请从最近工作说起，工作的单位，入职离职的时间以及工作内容";
        
        
    }
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 230;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
}


-(void)index:(NSInteger)index content:(NSString *)content
{
    if (index == 0) {
        self.education = content;
       
    }else{
        self.work = content;
    }
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 26;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%ld",row+1];
}


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 70;
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    self.workExperise.text = [NSString stringWithFormat:@"工作年限：%ld",row + 1];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    CGFloat progress = [change[@"new"] floatValue];
    dispatch_async(dispatch_get_main_queue(), ^(void){
        [SVProgressHUD showProgress:progress];
    });
    
}
@end
